﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Macds.Core.ServiceLocator;

namespace Macds.Test.Core.ServiceLocator.Mocks
{
public class DependencyFactory
{
    private Dictionary<string, ServiceElement> services;

    public DependencyFactory()
    {
        this.services = new Dictionary<string, ServiceElement>();
    }

    public T CreateService<T>(string name)
    {
        var element = GetServiceElement(name);
        var target = new LocatorTarget(element.Assembly, element.Class);
        var locator = new Locator(target);

        return (T)locator.Service;
    }

    private ServiceElement GetServiceElement(string name)
    {
        if (!services.ContainsKey(name))
        {
            var element = ServiceConfigurationSection.Load().Services[name];
            services.Add(name, element);
        }

        return services[name];
    }
}
}
